package com.chenliang.dao;

import com.chenliang.domain.Permission;
import com.chenliang.domain.Role;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @ProjectName: mavenSSM
 * @Package: com.chenliang.dao
 * 作用：
 */
public interface IRoledao {
@Select("select * from role where id in(select roleId from userInfo_role where userId=#{userId} )")
@Results(value = {
        @Result(id = true,property = "id",column = "id"),
        @Result(property = "permissions",column = "id",javaType = java.util.List.class,many=@Many(select = "com.chenliang.dao.IPermission.findById",fetchType = FetchType.LAZY)),
})
    List<Role> findByuserInfoid();



//通过权限查找角色再找用户,permission详细信息

        @Select("select * from role where id in(select roleId from role_permission where permissionId=#{permissionId} )")
        @Results(value = {
                @Result(id = true, property = "id", column = "id"),
                @Result(property = "users", column = "id", javaType = java.util.List.class, many = @Many(select = "com.chenliang.dao.IUsersInfo.findByRoleId", fetchType = FetchType.LAZY)),
        })
        List<Role> findByPermissionId();






@Select("select * from Role ")
List<Role> findAll();


//添加角色
@Insert("insert into ROLE(id,roleName,roleDesc)values(SYS_GUID,#{roleName},#{roleDesc}) ")
    void addRole(Role role)throws  Exception;



//角色和权限
@Select("select *from role where id=#{id}")
@Results(value = {
        @Result(id=true,property = "id",column = "id"),
        @Result(property = "permissions",column = "id",javaType =java.util.List.class,many = @Many(select = "com.chenliang.dao.IPermission.findById",fetchType = FetchType.LAZY))
})
    Role findById(String id);


@Select("select * from permission where id not in(select permissionId  from role_permission where roleId =#{id})")
    List<Permission> RolePermission(String id);


//角色添加权限
    @Insert("insert into role_permission values(#{permissionId},#{roleId}) ")
    void addPermission(@Param("roleId") String roleId,@Param("permissionId") String permissionId);
}
